﻿using System.Drawing;
using System.Windows.Forms;
using Controller;

namespace Reversi
{
	public partial class UCField : UserControl
	{
		private Token token;
		SolidBrush fillColor;
		private int column;
		private int row;

		public UCField(int column, int row, Token s = Token.Default)
		{
			this.column = column;
			this.row = row;
			this.token = s;
			SetTokenColor();
			InitializeComponent();
			this.ResizeRedraw = true;
		}

		#region FieldPaint

		private FieldStatus fieldStatus;

		public FieldStatus FieldStatus
		{
			get { return fieldStatus; }
			set { fieldStatus = value; HighlightField(fieldStatus); }
		}

		private void HighlightField(FieldStatus fieldStatus)
		{
			switch (fieldStatus)
			{
				case FieldStatus.Highlighted:
					this.BackColor = Color.LightGreen;
					break;
				case FieldStatus.Default:
					this.BackColor = Color.Green;
					break;
				case FieldStatus.LastSet:
					this.BackColor = Color.DarkOrange;
					break;
			}
		}

		#endregion

		#region TokenPaint

		private void UCField_Paint(object sender, PaintEventArgs e)
		{
			Graphics graph = e.Graphics;
			graph.FillEllipse(fillColor, 0, 0, this.ClientSize.Width, this.ClientSize.Height);
		}

		public Token Token
		{
			get { return token; }
			set { this.token = value; SetTokenColor(); this.Invalidate(); }
		}

		private void SetTokenColor()
		{
			switch (token)
			{
				case Token.Default:
					fillColor = new SolidBrush(Color.Green);
					break;
				case Token.Player1:
					fillColor = new SolidBrush(Color.Black);
					break;
				case Token.Player2:
					fillColor = new SolidBrush(Color.White);
					break;
			}
		}

		#endregion

		#region Eventhandler

		public delegate void FieldClickedEventHandler(object sender, FieldClickedEventArgs e);

		public event FieldClickedEventHandler FieldClicked;

		protected void OnFieldClicked()
		{
			if (FieldClicked != null)
			{
				FieldClickedEventArgs e = new FieldClickedEventArgs();
				e.Column = column;
				e.Row = row;
				FieldClicked(this, e);
			}
		}
		private void UCField_MouseClick(object sender, MouseEventArgs e)
		{
			OnFieldClicked();
		}

		#endregion

	}
}
